package manage.service.impl;

import com.github.pagehelper.PageHelper;
import core.enums.ResultCode;
import core.exception.ServiceException;
import manage.domain.user.User;
import manage.domain.user.dto.UserQueryDTO;
import manage.domain.user.vo.UserVO;
import manage.manager.UserCacheManage;
import manage.mapper.UserMapper;
import manage.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserCacheManage userCacheManage;
    @Override
    public List<UserVO> list(UserQueryDTO userQueryDTO) {
        PageHelper.startPage(userQueryDTO.getPageNum(), userQueryDTO.getPageSize());
        return userMapper.selectUserList(userQueryDTO);
    }

    @Override
    public int updateStatus(Long userId, Integer status) {
        User user = userMapper.selectById(userId);
        if(user == null) {
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        user.setStatus(status.byteValue());
        userCacheManage.updateStatus(userId, status);
        return userMapper.updateById(user);
    }
}
